package com.turing.post.transaction.util;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;

import com.turing.post.util.PCPSUtil;

@SuppressWarnings("serial")
public class ChkAccFile implements Serializable {

	public static final String XML_CHARSET = "gb18030";
	public static final String XML_CHECKFILE = "CheckFile";
	public static final String XML_MERCHANTNO = "MerchantNo";
	public static final String XML_TRANDATE = "TranDate";

	public static final String XML_SUMMARY = "Summary";

	public static final String XML_DEBITAMOUNT = "DebitAmount";
	public static final String XML_DEBITCOUNT = "DebitCount";
	public static final String XML_CREDITAMOUNT = "CreditAmount";
	public static final String XML_CREDITCOUNT = "CreditCount";

	public static final String XML_DETAILS = "Details";
	public static final String XML_DETAILNUMS = "DetailNums";
	public static final String XML_DETAIL = "Detail";
	public static final String XML_REQSEQNO = "ReqSeqNo";
	public static final String XML_ACKSEQNO = "AckSeqNo";
	public static final String XML_AMOUNT = "Amount";
	public static final String XML_TRANTYPE = "TranType";

	/**
	 * 商户号
	 */
	private String merchantNo;

	public String getMerchantNo() {
		return merchantNo;
	}

	public void setMerchantNo(String merchantNo) {
		this.merchantNo = merchantNo;
	}

	/**
	 * 帐务日期
	 */
	private Date tranDate;

	public Date getTranDate() {
		return tranDate;
	}

	public void setTranDate(Date tranDate) {
		this.tranDate = tranDate;
	}

	/**
	 * 借方笔数
	 */
	private BigInteger debitCount;

	public BigInteger getDebitCount() {
		return debitCount;
	}

	public void setDebitCount(BigInteger debitCount) {
		this.debitCount = debitCount;
	}

	/**
	 * 借方金额
	 */
	private BigInteger debitAmount;

	public BigInteger getDebitAmount() {
		return debitAmount;
	}

	public void setDebitAmount(BigInteger debitAmount) {
		this.debitAmount = debitAmount;
	}

	/**
	 * 贷方笔数
	 */
	private BigInteger creditCount;

	public BigInteger getCreditCount() {
		return creditCount;
	}

	public void setCreditCount(BigInteger creditCount) {
		this.creditCount = creditCount;
	}

	/**
	 * 贷方金额
	 */
	private BigInteger creditAmount;

	public BigInteger getCreditAmount() {
		return creditAmount;
	}

	public void setCreditAmount(BigInteger creditAmount) {
		this.creditAmount = creditAmount;
	}

	/**
	 * 文件明细行
	 */
	private ArrayList<ChkAccFileDetail> fileDetail = new ArrayList<ChkAccFileDetail>();

	public ArrayList<ChkAccFileDetail> getFileDetail() {
		return fileDetail;
	}

	public void setFileDetail(ArrayList<ChkAccFileDetail> fileDetail) {
		this.fileDetail = fileDetail;
	}

	/**
	 * 文件内容
	 */
	private byte[] FileContent;

	public byte[] getFileContent() {
		return FileContent;
	}

	public void setFileContent(byte[] fileContent) {
		FileContent = fileContent;
	}

	public ChkAccFile getSummaryFromDetail() {
		ChkAccFile caf = new ChkAccFile();

		BigInteger debitcount = BigInteger.ZERO;
		BigInteger debitamount = BigInteger.ZERO;
		BigInteger creditcount = BigInteger.ZERO;
		BigInteger creditamount = BigInteger.ZERO;

		for (ChkAccFileDetail cafd : fileDetail) {
			if (PCPSUtil.compareObject(cafd.getTranType(),
					ChkAccFileDetail.PAID)) {
				debitcount = debitcount.add(BigInteger.ONE);
				debitamount = debitamount.add(cafd.getAmount());
			} else if (PCPSUtil.compareObject(cafd.getTranType(),
					ChkAccFileDetail.PAIDCANCEL)) {
				debitcount = debitcount.add(BigInteger.ONE);
				debitamount = debitamount.subtract(cafd.getAmount());
			} else {
				creditcount = creditcount.add(BigInteger.ONE);
				creditamount = creditamount.add(cafd.getAmount());
			}
		}

		caf.setDebitCount(debitcount);
		caf.setDebitAmount(debitamount);
		caf.setCreditCount(creditcount);
		caf.setCreditAmount(creditamount);

		return caf;
	}

	/**
	 * 判断汇总信息是否一致(金额及笔数)
	 * 
	 * @param sCaf
	 * @param dCaf
	 * @return
	 */
	public static boolean isEqual(ChkAccFile sCaf, ChkAccFile dCaf) {
		return (sCaf.getDebitCount().equals(dCaf.getDebitCount())
				&& sCaf.getDebitAmount().equals(dCaf.getDebitAmount())
				&& sCaf.getCreditCount().equals(dCaf.getCreditCount()) && sCaf
				.getCreditAmount().equals(dCaf.getCreditAmount()));
	}

	/**
	 * 判断汇总金额信息是否一致(金额)
	 * 
	 * @param sCaf
	 * @param dCaf
	 * @return
	 */
	public static boolean isAmountEqual(ChkAccFile sCaf, ChkAccFile dCaf) {
		return (sCaf.getDebitAmount().add(sCaf.getCreditAmount()).equals(dCaf
				.getDebitAmount().add(dCaf.getCreditAmount())));
	}
}
